package com.wtwd.campus.jdzc.service.impl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.LocationMapper;
import com.wtwd.campus.entity.LocationData;
import com.wtwd.campus.jdzc.service.JdzcLocationService;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.GouldUtil;
import com.wtwd.campus.vcom.entity.VcomLocationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JdzcLocationServiceImpl implements JdzcLocationService {
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private GouldUtil gouldUtil;
    @Override
    public List<Map<String, Object>> getClassesLocationList(Integer classId,String imei,String orgCode) {
        List<Map<String, Object>> classesLocationList = locationMapper.getClassesLocationList(classId,imei,orgCode);
        for(Map<String,Object> map:classesLocationList){
            String deviceImei=(String)map.get("imei");
            //判读设备是否在线
            boolean b = redisService.hasKey(Constants.DEVICE_ONLINE_PREFIX.concat(deviceImei));
            if(b){
                map.put("on_off",1);//在线
            }else{
                map.put("on_off",2);//离线
            }
        }
        return classesLocationList;
    }

    @Override
    public List<Map<String, Object>> getDeviceTrack(String key, Long time,String orgCode) {
        Long maxTime=time+24*60*60*1000;
        List<Map<String, Object>> deviceTrack = locationMapper.getDeviceTrack(key, time, maxTime,orgCode);
        for(Map<String,Object> map:deviceTrack){
            String deviceImei=(String)map.get("imei");
            //判读用户是否在线
            boolean b = redisService.hasKey(Constants.DEVICE_ONLINE_PREFIX.concat(deviceImei));
            if(b){
                map.put("on_off",1);//在线
            }else{
                map.put("on_off",2);//离线
            }
        }
        return deviceTrack;
    }

    @Override
    public Integer addLocationData(LocationData data) {
        return locationMapper.addLocationData(data);
    }

    @Override
    public LocationData getLatestLocationData(Integer deviceId) {
        return locationMapper.getLatestLocationData(deviceId);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceId(Integer deviceId, Long time) {
        Long maxTime=24*60*60*1000+time;
        return locationMapper.getDeviceTrackByDeviceId(deviceId,time,maxTime);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceIdWx(Integer deviceId, Long time) {
        Long maxTime=24*60*60*1000+time;
        return locationMapper.getDeviceTrackByDeviceIdWx(deviceId,time,maxTime);
    }

    @Override
    public VcomLocationData getVcomLatestLocationData(Integer deviceId) {
        return locationMapper.getVcomLatestLocationData(deviceId);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceIdVcom(Integer deviceId, Long time) {
        Long maxTime=24*60*60*1000+time;
        return locationMapper.getDeviceTrackByDeviceIdVcom(deviceId,time,maxTime);
    }
}
